<?php
date_default_timezone_set('Asia/Shanghai');//时区配置
$conn=mysql_connect("121.37.61.76","idcnewgsdaimysq1","idcnewgsdmysq1!@#$*.mysq1");
include 'functions.php';
mysql_select_db("new_gsdai", $conn);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

$sql = "SELECT * FROM ych_borrow_tender WHERE borrow_id=2620";
$result = db_fetch_arrays($sql, $conn);
foreach ($result as $key => $value){
	$account_result_reward 	= db_fetch_array("select * from ych_account where user_id={$value['user_id']}", $conn);
	$log['user_id'] 		= $value['user_id'];
	$log['ftype'] 			= 48;
	$log['money'] 			= round($value['account']*0.036, 2);
	$log['total'] 			= $account_result_reward['total']-$log['money'];
	$log['use_money'] 		= $account_result_reward['use_money']-$log['money'];
	$log['no_use_money'] 	= $account_result_reward['no_use_money'];
	$log['collection'] 		= $account_result_reward['collection'];
	$log['to_user'] 		= "3573";
	$log['remark'] 			= "扣除借款[<a href=\'/borrow/obj/2620.html\'>管理咨询公司资金周转</a>]的奖励";
	$log['add_time'] 		= time();
	$log['addip'] 			= '127.0.0.1';
	$mode = $value['user_id']%100;
	$sql  ="insert into ych_account_log_$mode(user_id,ftype,total,money,use_money,no_use_money,
													collection,to_user,remark,add_time,addip) values( 
				 {$value['user_id']},{$log['ftype']},{$log['total']},{$log['money']},
				 {$log['use_money']},{$log['no_use_money']},
				 {$log['collection']},{$log['to_user']},'{$log['remark']}',
				 {$log['add_time']},'{$log['addip']}'
				 )";
	$query = mysql_query($sql, $conn);
	if(!$query){
		echo $sql;
		exit;
	}
	$query = mysql_query("update ych_account set total=total-{$log['money']},
				 use_money=use_money-{$log['money']} where user_id={$value['user_id']}", $conn);
	if(!$query){
		echo $sql;
		exit;
	}
	
	unset($log);
	$account_result_coll 	= db_fetch_array("select * from ych_account where user_id={$value['user_id']}", $conn);
	$log['user_id'] 		= $value['user_id'];
	$log['ftype'] 			= 49;
	$log['money'] 			= round($value['repayment_account'], 2);
	$log['total'] 			= $account_result_coll['total']-$value['interest'];
	$log['use_money'] 		= $account_result_coll['use_money'];
	$log['no_use_money'] 	= $account_result_coll['no_use_money']+$value['account'];
	$log['collection'] 		= $account_result_coll['collection']-$log['money'];
	$log['to_user'] 		= "3573";
	$log['remark'] 			= "流转标撤标，减去待收金额";
	$log['add_time'] 		= time();
	$log['addip'] 			= '127.0.0.1';
	$mode = $value['user_id']%100;
	$sql  ="insert into ych_account_log_$mode(user_id,ftype,total,money,use_money,no_use_money,
						collection,to_user,remark,add_time,addip) values(
						{$value['user_id']},{$log['ftype']},{$log['total']},{$log['money']},
						{$log['use_money']},{$log['no_use_money']},
						{$log['collection']},{$log['to_user']},'{$log['remark']}',
						{$log['add_time']},'{$log['addip']}'
						)";
	$query = mysql_query($sql, $conn);
	if(!$query){
	echo $sql;
	exit;
	}
	$query = mysql_query("update ych_account set total=total-{$value['interest']},
							no_use_money=no_use_money+{$value['account']},
							collection=collection-{$value['repayment_account']} where user_id={$value['user_id']}", $conn);
	if(!$query){
			echo $sql;
			exit;
	}
	
	unset($log);
	$account_result_return 	= db_fetch_array("select * from ych_account where user_id={$value['user_id']}", $conn);
	$log['user_id'] 		= $value['user_id'];
	$log['ftype'] 			= 13;
	$log['money'] 			= round($value['account'], 2);
	$log['total'] 			= $account_result_return['total'];
	$log['use_money'] 		= $account_result_return['use_money']+$log['money'];
	$log['no_use_money'] 	= $account_result_return['no_use_money']-$log['money'];
	$log['collection'] 		= $account_result_return['collection'];
	$log['to_user'] 		= "3573";
	$log['remark'] 			= "投标失败返回资金";
	$log['add_time'] 		= time();
	$log['addip'] 			= '127.0.0.1';
	$mode = $value['user_id']%100;
	$sql  ="insert into ych_account_log_$mode(user_id,ftype,total,money,use_money,no_use_money,
			collection,to_user,remark,add_time,addip) values(
			{$value['user_id']},{$log['ftype']},{$log['total']},{$log['money']},
			{$log['use_money']},{$log['no_use_money']},
			{$log['collection']},{$log['to_user']},'{$log['remark']}',
			{$log['add_time']},'{$log['addip']}'
			)";
	$query = mysql_query($sql, $conn);
	if(!$query){
	echo $sql;
	exit;
	}
	$query = mysql_query("update ych_account set use_money=use_money+{$value['account']},
							no_use_money=no_use_money-{$value['account']} where user_id={$value['user_id']}", $conn);
	if(!$query){
	echo $sql;
	exit;
	}
	
	
	$query = mysql_query("update ych_borrow set account_yes=account_yes-{$value['account']},tender_times=tender_times-1
							where id=2620", $conn);
	if(!$query){
	echo $sql;
	exit;
	}
	
	$query = mysql_query("update ych_borrow_collection set fstatus=2 where tender_id={$value['id']};", $conn);
	if(!$query){
	echo $sql;
	exit;
	}
	
	$query = mysql_query("update ych_borrow_tender set fstatus=2 where id={$value['id']};", $conn);
	if(!$query){
	echo $sql;
	exit;
	}
}


$query = mysql_query("update ych_borrow set fstatus=2 where id=2620", $conn);
if(!$query){
	echo $sql;
	exit;
}
echo "over";
